//3.class  es6新增语法糖
//通过class的关键字来定义类
//看起来像类，实际还是构造函数，只是写法更清晰

//通过constructor构造器书写属性，类名约定俗成大写
class Cat {
    constructor(name = 'xiaohua', age = 1) {
        this.name = name;
        this.age = age;
    }
    talk() {
        console.log(this.name);
    }
}

let cat1 = new Cat('咪咪', 2);//实例化对象
cat1.talk();//调用对象的方法
cat1.color = 'white';
cat1.walk = function () {
    console.log('gogogo~~');
}
cat1.walk()
console.log(cat1);

let cat2 = new Cat('huahua',4);
console.log(cat2);
cat2.talk()
// cat2.walk()//报错


console.log(cat1.__proto__ == cat2.__proto__);//true
console.log(cat1.__proto__ == Cat.prototype);//true
console.log(cat2.__proto__ == Cat.prototype);//true
console.log(cat2.__proto__.constructor);//cat2.__proto__=>Cat.prototype

// 练习：创建一个人类、一个狗类
//打印我叫XXX,我有一直小狗叫XXX;
//人和狗的关系

class Dog {
    constructor(name, color) {
        this.name = name;
        this.color = color;
        this.talk();
    }
    talk() {
        console.log('我可以吃巧克力味的💩，但是不能吃💩味的巧克力');
    }
}
let dog = new Dog('来福', 'white&&black');
class Person {
    constructor(name, pet, age = 25) {
        this.name = name;
        this.age = age
        this.pet = pet;
        this.talk();//new出对象时，立即调用方法
    }
    talk() {
        console.log(`我叫${this.name},我有一只小狗叫${this.pet.name}`);
    }
}

let changwei = new Person('changwei', dog);
// changwei.talk();